//app.js
App({
  onLaunch: function () {
    let _this = this
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log('登录',res)
        wx.request({
          url: this.globalData.url+'/customer/wechat/get/code',
          data:{
            appid:'wx0e1975149f72ec4d',
            jsCode:res.code
          },
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          method:'post',
          success(res){
            console.log('res',res);
            _this.globalData.openId = res.data.openid
            _this.globalData.MALL_USER_COOKIE = res.data.MALL_USER_COOKIE
            _this.globalData.MALL_WECHAT_COOKIE = res.data.MALL_WECHAT_COOKIE
            // 获取用户信息
            wx.getSetting({
              success: res => {
                console.log('用户授权信息', res)
                if (res.authSetting['scope.userInfo']) {
                  // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                  wx.getUserInfo({
                    success: res => {
                      console.log(res)
                      // 可以将 res 发送给后台解码出 unionId
                      _this.globalData.userInfo = res.userInfo

                      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                      // 所以此处加入 callback 以防止这种情况
                      if (_this.userInfoReadyCallback) {
                        _this.userInfoReadyCallback(res.userInfo)
                      }

                      wx.request({
                        //保存用户信息
                        url: _this.globalData.url + '/customer/wechat/save/user',
                        method: 'post',
                        data: {
                          openid: _this.globalData.openId,
                          nickName: _this.globalData.userInfo.nickName,
                          avatarUrl: _this.globalData.userInfo.avatarUrl,
                          gender: _this.globalData.userInfo.gender,
                          city: _this.globalData.userInfo.city,
                          province: _this.globalData.userInfo.province
                        },
                        header: {
                          "content-type": "application/x-www-form-urlencoded",
                          "MALL_USER_COOKIE":_this.globalData.MALL_USER_COOKIE,
                          "MALL_WECHAT_COOKIE":_this.globalData.MALL_WECHAT_COOKIE
                        },
                        success(res) {
                          console.log('保存用户信息', res)
                          console.log('appid',_this.globalData.openId)
                        }
                      })
                    }
                  })
                }else{
                  wx.authorize({
                    scope: 'scope.userInfo',
                    success(res) {
                      console.log(res)
                    }
                  })
                }
              }
            })
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    url:'http://60.205.176.114:8084/mall-miniprogram-api',
    openId:'',
    MALL_WECHAT_COOKIE:'',
    MALL_USER_COOKIE:''
  }
})